/*******************************************************\
|* Author: Trevor Sundberg
\*******************************************************/

// Includes
#include "SkugoConsole.hpp"

namespace Skugo
{
  //***************************************************************************
  // The name of the remote type we wish to instantiate.
  static const string RemoteType = "Skugo.StandardPlugins.Console";

  //***************************************************************************
  Console::Console(Client* client, const string& displayName) :
    DockablePlugin(RemoteType, client, displayName)
  {
  }

  //***************************************************************************
  void Console::OnReceive(const char* data, sizeType size)
  {
    Buffer incoming(data, size);

    auto message = (MessageType)incoming.Read<unsigned char>();

    switch (message)
    {
      case UserInput:
      {
        auto text = incoming.ReadAsciiString();

        this->OnConsoleInput.Invoke(&text);
        break;
      }
    }
  }

  //***************************************************************************
  void Console::Write(const string& text)
  {
    // Create a binary writer to write the data
    Buffer outgoing;

    outgoing.Write((unsigned char)WriteMessage);
    outgoing.WriteAsciiString(text);

    this->Send(outgoing);
  }

  //***************************************************************************
  void Console::WriteLine(const string& text)
  {
    this->Write(text + "\n");
  }
}
